package com.cloud.wms.ui.inside.presenter;

import com.cloud.core.base.BaseRxPresenter;
import com.cloud.core.utils.MessageCreator;
import com.cloud.core.utils.NumberUtils;
import com.cloud.wms.app.FineExApplication;
import com.cloud.wms.http.Params;
import com.cloud.wms.http.observer.LoadingObserver;
import com.cloud.wms.http.observer.ResponseWrapper;
import com.cloud.wms.tools.RxUtils;
import com.cloud.wms.ui.inside.contract.ReplenishApplyContact;
import com.cloud.wms.ui.inside.entity.RecordGoodsEntity;

import javax.inject.Inject;

/**
 * Created by FC on 2021/3/12.
 * Desc：补货申请
 */
public class ReplenishApplyPresenter extends BaseRxPresenter<ReplenishApplyContact.View> implements ReplenishApplyContact.Presenter {
    public static final int GET_POS_COMMODITY_MSG = 0x100;
    public static final int CONFIRM_REPLENISH_SUCCESS = 0x101;
    public static final int CHECK_POS = 0x102;

    @Inject
    public ReplenishApplyPresenter() {

    }

    @Override
    public void checkReplenishApplyPos(String posCode) {
        Params params = Params.newInstance(Params.Type.WAREHOUSE);
        params.put("PosCode", posCode);
        FineExApplication
                .component()
                .http()
                .checkReplenishApplyPos(params)
                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<Object>(mView.provideContext()) {
                    @Override
                    public void onSuccess(Object info) {
                        mView.onSuccess(MessageCreator.createMessage(posCode, CHECK_POS));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }

    /**
     * 获取库位商品信息
     *
     * @param posCode
     * @param barCode
     */
    @Override
    public void getPosCommodityMsg(String posCode, String barCode, int stockType) {
        Params params = Params.newInstance(Params.Type.MEMBER);
        params.put("PosCode", posCode);
        params.put("BarCode", barCode);
        params.put("StockType", stockType);
        FineExApplication
                .component()
                .http()
                .getPosCommodityMsg(params)
                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<RecordGoodsEntity>(mView.provideContext()) {
                    @Override
                    public void onSuccess(RecordGoodsEntity goodsEntity) {
                        mView.onSuccess(MessageCreator.createMessage(goodsEntity, GET_POS_COMMODITY_MSG));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }

    /**
     * 确认提交补货申请
     *
     * @param recordGoodsEntity
     * @param Amount
     * @param IsUrgent          1紧急 0不紧急
     */
    @Override
    public void confirmReplenish(RecordGoodsEntity recordGoodsEntity, String Amount, int IsUrgent) {
        Params params = Params.newInstance(Params.Type.MEMBER);
        params.put("posID", recordGoodsEntity.getPosID());
        params.put("commodityID", recordGoodsEntity.getCommodityId());
        params.put("amount", NumberUtils.getIntegerValue(Amount));
        params.put("stockType", recordGoodsEntity.getStockType());
        params.put("isUrgent", IsUrgent);
        params.put("userID", FineExApplication.component().loginCache().getUserInfo().getUserID());
        FineExApplication
                .component()
                .http()
                .confirmReplenish(params)
                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<Object>(mView.provideContext()) {
                    @Override
                    public void onSuccess(Object s) {
                        mView.onSuccess(MessageCreator.createMessage(s, CONFIRM_REPLENISH_SUCCESS));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }
}
